<template>
  <div class="member-order">
    <!-- tab组件 -->
    <XtxTabs v-model="activeName" @tab-click="tabClick">
      <XtxTabsPanel
        v-for="item in orderStatus"
        :key="item.name"
        :label="item.label"
        :name="item.name"
      >
      </XtxTabsPanel>
    </XtxTabs>
    <!-- 订单列表 -->
    <div class="order-list">
      <!-- 加载中效果 -->
      <div v-if="loading" class="loading"></div>
      <!-- 无数据显示的文本 -->
      <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
      <!-- 订单组件 -->
      <OrderItem v-for="item in orderList"
      :key="item.id"
      :order="item"
      @on-cancel="handlerCancel"
      @on-delete="handlerDelete"
      @on-confirm="handlerConfirm"
      @on-logistics="handlerLogistics"
      />
    </div>
    <!-- 分页组件 -->
    <XtxPagination
    v-if="orderList.length>0"
    :current-page="reqParams.page"
      :page-size="reqParams.pageSize"
      :total="total"
      @current-change="reqParams.page=$event"
    />
    <!-- 取消订单提示框 -->
    <OrderCancel ref="orderCancelCom"/>
    <!-- 查看物流组件 -->
    <OrderLogistics ref="orderLogisticsCom"/>
  </div>
</template>
<script>
import { reactive, ref, watch } from 'vue'
import { orderStatus } from '@/api/constants'
import OrderItem from './components/order-item'
import OrderCancel from './components/order-cancel'
import OrderLogistics from './components/order-logistics'
import { confirmOrder, deleteOrder, findOrderList } from '@/api/order'
import Confirm from '@/components/library/Confirm'
import Message from '@/components/library/Message'
export default {
  name: 'MemberOrder',
  components: {
    OrderItem,
    OrderCancel,
    OrderLogistics
  },
  setup () {
    // tab选项
    const activeName = ref('all')

    // 控制加载效果显示的数据
    const loading = ref(false)

    // 监听选项数据变化时，调接口获取对应订单数据
    const reqParams = reactive({
      page: 1,
      pageSize: 5,
      orderState: 0
    })
    // 数据总数
    const total = ref(0)
    const orderList = ref([])
    // 封装请求订单数据函数
    const getOrderList = () => {
      loading.value = true
      findOrderList(reqParams).then(data => {
        orderList.value = data.result.items
        total.value = data.result.counts
        loading.value = false
      })
    }
    watch(reqParams, () => {
      getOrderList()
    }, { immediate: true })

    // 获取当前tab选项索引
    const tabClick = ({ index }) => {
      // 切换tab页码都是重置为1
      reqParams.page = 1
      reqParams.orderState = index
    }

    // 删除订单操作
    const handlerDelete = (order) => {
      // 提示
      Confirm({ text: '亲，您确认删除该订单吗？' }).then(() => {
        // 调接口删除
        deleteOrder(order.id).then(data => {
          Message({ type: 'success', text: '删除成功' })
          // 调接口请求最新订单数据
          getOrderList()
        })
      }).catch(() => {})
    }
    return {
      activeName,
      orderStatus,
      orderList,
      tabClick,
      loading,
      total,
      reqParams,
      handlerDelete,
      ...useCancel(),
      ...useConfirm(),
      ...useLogistics()
    }
  }
}

// 取消订单逻辑
export const useCancel = () => {
  // 通过return形式，返回给setup函数里面调用
  // 提示框实例
  const orderCancelCom = ref(null)
  const handlerCancel = (order) => {
  // 显示取消提示框
    orderCancelCom.value.open(order)
  }
  return { handlerCancel, orderCancelCom }
}

// 确认收货逻辑
export const useConfirm = () => {
  const handlerConfirm = (order) => {
    Confirm({ text: '亲，您确认收货嘛？' }).then(() => {
      // 调接口，确认商品收货
      confirmOrder(order.id).then(data => {
        Message({ type: 'success', text: '确认收货成功' })
        // 更改商品状态为待评价
        order.orderState = 4
      })
    }).catch(() => {})
  }
  return { handlerConfirm }
}

// 查看物流逻辑
export const useLogistics = () => {
  // 查看物流组件实例
  const orderLogisticsCom = ref(null)
  const handlerLogistics = (order) => {
    orderLogisticsCom.value.open(order)
  }
  return { handlerLogistics, orderLogisticsCom }
}

</script>
<style scoped lang="less">
.member-order {
  height: 100%;
  background: #fff;
}
.order-list {
  background: #fff;
  padding: 20px;
  position: relative;
  min-height: 400px;
}
.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255,255,255,.9) url(../../../assets/images/loading.gif) no-repeat center;
}
.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>
